/*
 * @lc app=leetcode.cn id=56 lang=typescript
 *
 * [56] 合并区间
 */

// @lc code=start
function merge(intervals: number[][]): number[][] {
    if (intervals.length === 0) {
        return [];
    }
    intervals.sort((a,b)=>a[0] - b[0])
    let res: any = [];
    for (let i = 0; i < intervals.length; i++) {
        const t = intervals[i];
        if (res.length === 0) {
            res.push(t);
            continue;
        }
        const l = res[res.length - 1];
        if (t[0] <= l[0] && t[1] >= l[0]) {
            l[0] = t[0];
        }
        if (t[1] > l[1] && t[0] <= l[1]) {
            l[1] = t[1];
        }
        if (t[1] < l[0] || t[0] > l[1]) {
            res.push(t);
        }
    }
    // console.log(res)
    return res;
}

// merge([[2,3],[4,5],[6,7],[8,9],[1,10]]);
// @lc code=end
